﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Common;
using System.Drawing;
using System.IO;

namespace YJKDataBase
{
	[DefaultProperty("Category")]
	public class DbCategory : Parent
	{
		[Browsable(false)]
		public Guid ParentId
		{
			get
			{
				return this.mParentId;
			}
		}

		[Category("属性")]
		[Description("图片")]
		[DisplayName("图片")]
		public Image Image
		{
			get
			{
				return this.mImage;
			}
			set
			{
				this.mImage = value;
			}
		}

		public DbCategory()
		{
			this.mName = "";
			this.mParentId = Guid.Empty;
			this.mGuid = Guid.Empty;
		}

		public DbCategory(string cName, Guid pId)
		{
			this.mName = cName;
			this.mParentId = pId;
			this.mGuid = Guid.NewGuid();
		}

		public DbCategory(string cName)
		{
			this.mName = cName;
			this.mGuid = Guid.NewGuid();
		}

		public void SetId(Guid id)
		{
			this.mGuid = id;
		}

		public void SetParentId(Guid pId)
		{
			this.mParentId = pId;
		}

		public void Remove()
		{
			if (base.Limit)
			{
				throw new Exception("无法删除系统分类");
			}
			List<DbObject> list = base.Database.ObjectTable[this];
			List<DbCategory> list2 = base.Database.CategoryTable[this];
			if (list.Count > 0)
			{
				foreach (DbObject dbObject in list)
				{
					dbObject.Remove();
				}
			}
			if (list2.Count > 0)
			{
				foreach (DbCategory dbCategory in list2)
				{
					dbCategory.Remove();
				}
			}
			base.Database.CategoryTable.Remove(this);
		}

		public bool ReName(string newName)
		{
			if (base.Limit)
			{
				throw new Exception("系统分类无法改名");
			}
			this.mName = newName;
			return base.Database.CategoryTable.Update(this);
		}

		public bool ReParent(Parent newParent)
		{
			if (base.Limit)
			{
				throw new Exception("系统分类无法移动");
			}
			Guid parentId = this.ParentId;
			this.SetParentId(newParent.Guid);
			bool result;
			try
			{
				base.Database.CategoryTable.Update(this);
				result = true;
			}
			catch
			{
				this.SetParentId(parentId);
				result = false;
			}
			return result;
		}

		public List<DbCategory> GetAllParents()
		{
			List<DbCategory> result = new List<DbCategory>();
			while (this.GetParent(this, ref result))
			{
			}
			return result;
		}

		private bool GetParent(DbCategory category, ref List<DbCategory> cl)
		{
			return false;
		}

		public bool AddObject(DbObject dbObject, FileInfo file)
		{
			bool result;
			using (DbTransaction dbTransaction = this.mDatabase.StartTransaction())
			{
				try
				{
					base.Database.ObjectTable.Add(this, dbObject, dbTransaction);
					base.Database.ObjectFileTable.Add(dbObject, file, dbTransaction);
					dbObject.SetDatabase(this.mDatabase);
					dbObject.SetParentId(base.Guid);
					base.Database.CommitTransaction(dbTransaction);
					result = true;
				}
				catch
				{
					base.Database.RollbackTransaction(dbTransaction);
					throw new Exception("添加失败，请检查该分类下是否包含重名族");
				}
			}
			return result;
		}

		public DbCategory this[string cName]
		{
			get
			{
				DbCategory result = null;
				foreach (DbCategory dbCategory in base.GetAllChildren(true))
				{
					if (dbCategory.Name.Equals(cName))
					{
						result = dbCategory;
						break;
					}
				}
				return result;
			}
		}

		public DbCategory this[Guid guid]
		{
			get
			{
				DbCategory result = null;
				try
				{
					foreach (DbCategory dbCategory in base.GetAllChildren(true))
					{
						if (dbCategory.Guid.Equals(guid))
						{
							result = dbCategory;
							break;
						}
					}
				}
				catch
				{
				}
				return result;
			}
		}

		public DbCategory MainCategory()
		{
			DbCategory dbCategory = this;
			bool flag = DbCategory.testCategory(dbCategory);
			while (flag)
			{
				dbCategory = dbCategory.Database.CategoryTable[dbCategory.mParentId];
				flag = DbCategory.testCategory(dbCategory);
			}
			return dbCategory;
		}

		private static bool testCategory(DbCategory category)
		{
			return category.Database.CategoryTable[category.mParentId] != null;
		}

		public override string ToString()
		{
			return this.mName;
		}

		private Guid mParentId;

		private Image mImage;
	}
}
